package edu.corn.job.config;


import edu.corn.common.design.chain.Chain;
import edu.corn.job.core.context.JobRunningContext;
import edu.corn.job.core.engine.AsyncJobEngine;
import edu.corn.job.core.engine.JobEngine;
import edu.corn.job.core.engine.mutex.MutexPool;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;

@EnableConfigurationProperties({JobConfigProperties.class})
@Import(JobSupportAutoConfiguration.class)
public class CornJobStarterAutoConfiguration {

    @Resource
    @Qualifier("jobMutexPool")
    private MutexPool mutexPool;

    @Resource()
    @Qualifier("jobExecutorService")
    private ExecutorService executorService;

    @Resource
    private HashMap<String, Chain<JobRunningContext>> chainHashMap;

    @Resource
    JobConfigProperties jobConfigProperties;

    @Bean("AsyncJobEngine")
    public JobEngine jobEngine() {
        JobEngine job = new AsyncJobEngine(executorService);
        job.init(mutexPool, "AsyncJobEngine", chainHashMap, jobConfigProperties);
        return job;
    }
}
